﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace NWBA_Public
{

    public class CustomerRepository
    {
        public static SqlConnection con = new SqlConnection(ServerString.getServerString());
        public CustomerRepository()
        {

        }
        public static Customer SelectOneCustomer(int id)
        {
            try
            {
                SqlCommand command = new SqlCommand("dbo.GetCust", con);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("ID", SqlDbType.Int).Value = id;
                con.Open();
                SqlDataReader sqlr = command.ExecuteReader();
                while (sqlr.Read())
                {
                    Customer newCust = new Customer(sqlr.GetInt32(0), sqlr.GetString(1), sqlr.GetString(2), sqlr.GetString(3), sqlr.GetString(4), sqlr.GetString(5), sqlr.GetString(6), sqlr.GetString(7));
                    return newCust;
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                con.Close();
            }            
        }
        public static List<Customer> Select(int id)
        {
            try
            {
                List<Customer> _Cust = new List<Customer>();
                SqlCommand command = new SqlCommand("dbo.GetCust", con);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("ID", SqlDbType.Int).Value = id;
                con.Open();
                SqlDataReader sqlr = command.ExecuteReader();
                while (sqlr.Read())
                {
                    Customer newCust = new Customer(sqlr.GetInt32(0), sqlr.GetString(1), sqlr.GetString(2), sqlr.GetString(3), sqlr.GetString(4), sqlr.GetString(5), sqlr.GetString(6), sqlr.GetString(7));
                    _Cust.Add(newCust);
                }
                return _Cust;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }
        public static void Update(int CustomerID, string CustomerName, string TFN, string address, string city, string state, string postCode, string phone)
        {
            try
            {

                con.Open();
                SqlCommand command = new SqlCommand("UPDATE [Customer] set CustomerName =@name, TFN=@TFN, Address=@address, City=@city, State=@state, PostCode=@postCode, Phone=@phone WHERE CustomerID= @ID", con);
                command.Parameters.AddWithValue("@ID", CustomerID);
                command.Parameters.AddWithValue("@name", CustomerName);
                command.Parameters.AddWithValue("@TFN", TFN);
                command.Parameters.AddWithValue("@Address", address);
                command.Parameters.AddWithValue("@City", city);
                command.Parameters.AddWithValue("@State", state);
                command.Parameters.AddWithValue("@postCode", postCode);
                command.Parameters.AddWithValue("@Phone", phone);
                //command.Parameters.AddWithValue("@date", DateTime.Now);
                command.ExecuteNonQuery();
            }
            catch (Exception o)
            {
            }
            finally
            {
                con.Close();
            }
        }
    }
}